package com.winteam.View;

import com.winteam.Bean.Admin;
import com.winteam.Controler.Select;
import com.winteam.Controler.Updata;
import org.junit.Test;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login  extends JFrame {
       Admin admin ;
    static Select select = new Select();
    static Updata updata = new Updata();
    JComboBox box;
    public Login(){
        //标题
        this.setTitle("系统登录");
        //窗口大小
        this.setSize(450,320);
        //点击关闭 退出
        this.setDefaultCloseOperation(3);
        //控制屏幕在中间

        this.setLocationRelativeTo(null);// 表示展示在屏幕中间
        //不可改变大小
        this.setResizable(true);
        getContentPane().setLayout(null);
        ImageIcon icon=new ImageIcon(this.getClass().getResource("img/Login_top.gif"));//加载图片
        JLabel label = new JLabel(icon);// 把图片放进去
        label.setSize(450,icon.getIconHeight());

        this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));//获取窗口的第二层，将label放入
        JPanel j=(JPanel)this.getContentPane();
        this.setBackground(new Color(231, 215, 183));
        j.setOpaque(false); //获取frame的顶层容器,并设置为透明
        j.setBackground(new Color(231, 215, 183));

        JLabel uSerJLabel=new JLabel("用 户 名:");
        uSerJLabel.setBounds(30, 80, 100, 40);
        uSerJLabel.setFont(new Font("微软雅黑",Font.PLAIN, 16));
        getContentPane().add(uSerJLabel);
        //
        JLabel pwdJLabel=new JLabel("登录密码:");
        pwdJLabel.setBounds(30, 140, 100, 40);
        pwdJLabel.setFont(new Font("微软雅黑",Font.PLAIN, 16));
        getContentPane().add(pwdJLabel);
        //多选框

        box =new JComboBox();
        box.setBounds(140, 80, 200, 35);

        String [] list = select.selectUser();
        for (String s : list) {
            box.addItem(s);
        }

        box.setFont(new Font("宋体", Font.BOLD, 16));
        getContentPane().add(box);

        //密码框
        //密码输入框
        JPasswordField passwordField=new JPasswordField();
        passwordField.setBounds(140,140, 200, 35);
        getContentPane().add(passwordField);
        //创建图片容器并赋予图片路径
        ImageIcon icon1 = new ImageIcon(this.getClass().getResource("img/key.gif"));
        //创建按钮
        JButton button = new JButton("登录",icon1);
        //设置图片大小
        button.setBounds(80, 195, 120, 40);
        button.setFocusPainted(false);//去掉按钮周围的焦点框
    	button.setContentAreaFilled(false);//设置按钮透明背景
        getContentPane().add(button);
        //创建图片容器并赋予图片路径
        ImageIcon icon2 = new ImageIcon(this.getClass().getResource("img/exit.gif"));
        //创建按钮
        JButton button2 = new JButton("退出",icon2);
        //设置图片大小
        button2.setBounds(230, 195, 120, 40);
        button2.setFocusPainted(false);//去掉按钮周围的焦点框
     	button2.setContentAreaFilled(false);//设置按钮透明背景
        getContentPane().add(button2);

        JSeparator separator = new JSeparator();
        separator.setBounds(0, 250, 694, 16);
        getContentPane().add(separator);
        JSeparator separator_1 = new JSeparator();
        separator_1.setBounds(0, 268, 700, 2);
        getContentPane().add(separator_1);

            JButton tisi =new JButton("提示：请输入登录密码...");
            tisi.setBounds(0, 240, 300, 50);
            tisi.setContentAreaFilled(false);//设置按钮透明背景
            tisi.setFocusPainted(false);//去掉按钮周围的焦点框
            tisi.setBorderPainted(false);//去边框
            getContentPane().add(tisi);
            tisi.setBounds(0,247,300,25);

        //退出系统监听事件
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result = JOptionPane.showConfirmDialog(null,"您现在要关闭系统吗?","退出提示",0,1);
                if(result == JOptionPane.OK_OPTION){
                    JOptionPane.showMessageDialog(null, "已退出系统，欢迎下次使用！");
                    System.exit(0);
                }
            }
        });
        //登录监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account =(String) box.getSelectedItem();

                String pwd =passwordField.getText();

                System.out.println(pwd);
                if (pwd==null||pwd.equals("")){
                    JOptionPane.showMessageDialog(null,"请输入密码!");
                    return;
                }

                admin=select.select(account, pwd);

                if ( admin != null && admin.getPwd().equals(pwd)){
                    JOptionPane.showMessageDialog(null, "登录成功");
                    //跳转到另外的界面
                    MainJFrame admf=new MainJFrame(admin);
                    admf.setVisible(true);
                    dispose();//销毁
                }	else {
                    JOptionPane.showMessageDialog(null, "密码输入错误,登录失败");
                }
            }
        });

    }

    public static void main(String[] args) {
        new Login().setVisible(true);
    }


}
